package com.mymarkdown.app.fx.event.hander.impl;

import com.mymarkdown.app.fx.annotation.KeyWord;
import com.mymarkdown.app.fx.enums.FxEventEnum;
import com.mymarkdown.app.fx.event.hander.KeyWordHandler;
import com.mymarkdown.app.fx.event.hander.MenuHandler;
import com.mymarkdown.app.fx.event.model.FxEvent;
import com.mymarkdown.app.interfaces.UserInfoService;
import com.mymarkdown.app.websocket.enums.MessageTypeEnum;
import com.mymarkdown.app.websocket.interfaces.WebSocketSendMessage;
import com.mymarkdown.app.websocket.message.MessagePojo;
import javafx.scene.web.WebEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Objects;
import java.util.concurrent.BlockingQueue;

/**
 * @author zenglun
 * @date 2021/12/5 9:54 上午
 * @description: 新建操作的处理
 */
@Slf4j
@KeyWord(FxEventEnum.NEW_CREATE_MD)
@Component("newCreateFileMenuHandlerImpl")
public class NewCreateFileMenuHandlerImpl implements MenuHandler, KeyWordHandler {

    @Autowired
    private WebSocketSendMessage webSocketSendMessage;

    @Autowired
    private UserInfoService userInfoService;

    @Override
    public void handler(WebEngine engine, FxEvent event) {
        //首先清除
        BlockingQueue<File> operationFile = userInfoService.getOperationFile();
        //清空当前操作文件夹
        File peek = operationFile.peek();
        if (!Objects.isNull(peek)){
            operationFile.clear();
        }
        //下发清除指令给客户端页面
        MessagePojo messagePojo = new MessagePojo();
        messagePojo.setMessageType(MessageTypeEnum.CLEAR_MD_BODY.getMessageType());
        messagePojo.setBody("");
        webSocketSendMessage.sendMessage(messagePojo);

    }

    @Override
    public boolean afterHandler(WebEngine engine, FxEvent event) {
        return true;
    }

    @Override
    public void beforeHandler(WebEngine engine, FxEvent event) {

    }
}
